<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="关联课程班ID" prop="courseScheduleDetailsId">-->
<!--        <el-input-->
<!--          v-model="queryParams.courseScheduleDetailsId"-->
<!--          placeholder="请输入关联课程班ID"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="学生ID" prop="studentId">
        <el-input
          v-model="queryParams.studentId"
          placeholder="请输入学生ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="选课状态" prop="enrollmentStatus">
        <el-select v-model="queryParams.enrollmentStatus" placeholder="请选择选课状态" clearable>
          <el-option
            v-for="dict in enrollment_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="选课时间" prop="enrollmentTime">
        <el-date-picker clearable
          v-model="queryParams.enrollmentTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择选课时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="退课时间" prop="withdrawalTime">
        <el-date-picker clearable
          v-model="queryParams.withdrawalTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择退课时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="平时成绩" prop="attendanceScore">
        <el-input
          v-model="queryParams.attendanceScore"
          placeholder="请输入平时成绩"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="考试成绩" prop="examScore">
        <el-input
          v-model="queryParams.examScore"
          placeholder="请输入考试成绩"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总成绩" prop="totalScore">
        <el-input
          v-model="queryParams.totalScore"
          placeholder="请输入总成绩"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否及格" prop="isPassed">
        <el-select v-model="queryParams.isPassed" placeholder="请选择是否及格" clearable>
          <el-option
            v-for="dict in y_n"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="记录创建时间" prop="createdAt">
        <el-date-picker clearable
          v-model="queryParams.createdAt"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择记录创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="记录更新时间" prop="updatedAt">
        <el-date-picker clearable
          v-model="queryParams.updatedAt"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择记录更新时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['trainingManagement:CourseStudentInfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['trainingManagement:CourseStudentInfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['trainingManagement:CourseStudentInfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['trainingManagement:CourseStudentInfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="CourseStudentInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="关联课程班ID" align="center" prop="courseScheduleDetailsId" />
      <el-table-column label="学生ID" align="center" prop="studentId" />
      <el-table-column label="选课状态" align="center" prop="enrollmentStatus">
        <template #default="scope">
          <dict-tag :options="enrollment_status" :value="scope.row.enrollmentStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="选课时间" align="center" prop="enrollmentTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.enrollmentTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="退课时间" align="center" prop="withdrawalTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.withdrawalTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="平时成绩" align="center" prop="attendanceScore" />
      <el-table-column label="考试成绩" align="center" prop="examScore" />
      <el-table-column label="总成绩" align="center" prop="totalScore" />
      <el-table-column label="是否及格" align="center" prop="isPassed">
        <template #default="scope">
          <dict-tag :options="y_n" :value="scope.row.isPassed"/>
        </template>
      </el-table-column>
      <el-table-column label="记录创建时间" align="center" prop="createdAt" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="记录更新时间" align="center" prop="updatedAt" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.updatedAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['trainingManagement:CourseStudentInfo:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['trainingManagement:CourseStudentInfo:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改课程学生信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="CourseStudentInfoRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="关联课程班ID" prop="courseScheduleDetailsId">
          <el-input v-model="form.courseScheduleDetailsId" placeholder="请输入关联课程班ID" />
        </el-form-item>
        <el-form-item label="学生ID" prop="studentId">
          <el-input v-model="form.studentId" placeholder="请输入学生ID" />
        </el-form-item>
        <el-form-item label="选课状态 (0=待审核, 1=已选课, 2=已退课)" prop="enrollmentStatus">
          <el-select v-model="form.enrollmentStatus" placeholder="请选择选课状态 (0=待审核, 1=已选课, 2=已退课)">
            <el-option
              v-for="dict in enrollment_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="选课时间" prop="enrollmentTime">
          <el-date-picker clearable
            v-model="form.enrollmentTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择选课时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="退课时间" prop="withdrawalTime">
          <el-date-picker clearable
            v-model="form.withdrawalTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择退课时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="平时成绩" prop="attendanceScore">
          <el-input v-model="form.attendanceScore" placeholder="请输入平时成绩" />
        </el-form-item>
        <el-form-item label="考试成绩" prop="examScore">
          <el-input v-model="form.examScore" placeholder="请输入考试成绩" />
        </el-form-item>
        <el-form-item label="总成绩" prop="totalScore">
          <el-input v-model="form.totalScore" placeholder="请输入总成绩" />
        </el-form-item>
        <el-form-item label="是否及格" prop="isPassed">
          <el-select v-model="form.isPassed" placeholder="请选择是否及格">
            <el-option
              v-for="dict in y_n"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="记录创建时间" prop="createdAt">
          <el-date-picker clearable
            v-model="form.createdAt"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择记录创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="记录更新时间" prop="updatedAt">
          <el-date-picker clearable
            v-model="form.updatedAt"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择记录更新时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="CourseStudentInfo">
import { listCourseStudentInfo, getCourseStudentInfo, delCourseStudentInfo, addCourseStudentInfo, updateCourseStudentInfo } from "@/api/trainingManagement/CourseStudentInfo";

const { proxy } = getCurrentInstance();
const { y_n, enrollment_status } = proxy.useDict('y_n', 'enrollment_status');

const CourseStudentInfoList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    courseScheduleDetailsId: null,
    studentId: null,
    enrollmentStatus: null,
    enrollmentTime: null,
    withdrawalTime: null,
    attendanceScore: null,
    examScore: null,
    totalScore: null,
    isPassed: null,
    createdAt: null,
    updatedAt: null
  },
  rules: {
    courseScheduleDetailsId: [
      { required: true, message: "关联课程班ID不能为空", trigger: "blur" }
    ],
    studentId: [
      { required: true, message: "学生ID不能为空", trigger: "blur" }
    ],
    enrollmentStatus: [
      { required: true, message: "选课状态 (0=待审核, 1=已选课, 2=已退课)不能为空", trigger: "change" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询课程学生信息列表 */
function getList() {
  loading.value = true;
  listCourseStudentInfo(queryParams.value).then(response => {
    CourseStudentInfoList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    courseScheduleDetailsId: null,
    studentId: null,
    enrollmentStatus: null,
    enrollmentTime: null,
    withdrawalTime: null,
    attendanceScore: null,
    examScore: null,
    totalScore: null,
    isPassed: null,
    createdAt: null,
    updatedAt: null
  };
  proxy.resetForm("CourseStudentInfoRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加课程学生信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getCourseStudentInfo(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改课程学生信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["CourseStudentInfoRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateCourseStudentInfo(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addCourseStudentInfo(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除课程学生信息编号为"' + _ids + '"的数据项？').then(function() {
    return delCourseStudentInfo(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('trainingManagement/CourseStudentInfo/export', {
    ...queryParams.value
  }, `CourseStudentInfo_${new Date().getTime()}.xlsx`)
}

getList();
</script>
